package xyz.bali16.module.blog.controller;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import xyz.bali16.application.core.model.Result;
import xyz.bali16.module.blog.utils.PageUtils;
import xyz.bali16.module.blog.entity.Category;
import xyz.bali16.module.blog.model.vo.blog.CategoryVo;
import xyz.bali16.module.blog.service.CategoryService;


import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(BaseController.API+"blog/category")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;


    @GetMapping("/list")
    @ResponseBody
    @ApiOperation("目录列表数据")
    //@PreAuthorize("@auth.permission('plugin:blog:category:query')")
    public Result<PageUtils> list(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                  @RequestParam(value = "size", required = false, defaultValue = "20") Integer size) {
        List<Category> categorys = categoryService.list();
        PageUtils pages = new PageUtils(current, size, categorys);
        return Result.success(pages);
    }

    @RequestMapping("/add")
    @ResponseBody
    //@PreAuthorize("@auth.permission('plugin:blog:category:add')")
    public Result<Boolean> save(@RequestBody Category categoryForm) {
        boolean state = categoryService.save(categoryForm);
        return Result.success(state);
    }


    @PostMapping("/edit")
    @ResponseBody
    //@PreAuthorize("@auth.permission('plugin:blog:category:update')")
    public Result<Boolean> edit(@RequestBody Category categoryForm) {
        System.out.println("edit:category");
        System.out.println(categoryForm.toString());
        return Result.success(categoryService.updateById(categoryForm));
    }

    /**
     * 热门
     */
//    @GetMapping("/hot")
//    public R hot() {
//        List<Integer> categoryIds = articlecategoryService.queryHotcategoryIds(HOT_ARTICLE_category_NUM);
//        log.info("categoryIds.size()={} {}", categoryIds.size(), categoryIds.toString());
//        Integer[] categoryIdsArray = new Integer[categoryIds.size()];
//        List<category> categoryEntities = categoryService.queryHotcategoryDetails(categoryIds.toArray(categoryIdsArray));
//        return R.ok(categoryEntities);
//    }

    /**
     * 查询标签详情
     */
    @GetMapping("/detail")
    //@PreAuthorize("@auth.permission('plugin:blog:category:query')")
    public Result<List<CategoryVo>> detail() {
        List<CategoryVo> categoryVos = categoryService.queryCategoryDetails();
        return Result.success(categoryVos);
    }

    /**
     * 单条标签详情
     */
    @GetMapping("/{categoryId}")
    //@PreAuthorize("@auth.permission('plugin:blog:category:query')")
    public Result<CategoryVo> detailById(@PathVariable("categoryId") Long categoryId) {
        CategoryVo categoryVo = categoryService.queryOneCategoryDetail(categoryId);
        return Result.success(categoryVo);
    }


    /**
     * 删除
     */
    @PostMapping("/delete")
    @ResponseBody
    //@PreAuthorize("@auth.permission('plugin:blog:category:delete')")
    public Result<Boolean> delete(@RequestBody Map<Object, String> map) {
        Long id = Long.valueOf(map.get("id"));
        return Result.success(categoryService.removeById(id));
    }
}
